﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormOrderInfoForTempered : FormTemplet
    {
        private DataTable _dtOrderItems = null;

        private long _nOrderID = -1L;

        private string _sOrderCode = null;

        private string _sCompanyName = null;

        private string _sCompanyLinkman = null;

        private string _sProjectName = null;

        private string _sProjectLinkman = null;

        private string _sProjectLinkmanPhone = null;

        private string _sProjectAddress = null;

        private string _sSpeakForDate = null;

        private string _sDeliveryDate = null;

        private bool _isLocked = true;

        private bool _isCancelled = false;

        private string _sRemark = null;

        private string _sOperatorName = null;

        private string _sOriginalQuantity = "0";

        private string _sOriginalValueForFinish = "0";

        public FormOrderInfoForTempered()
        {
            this.InitializeComponent();
        }

        public FormOrderInfoForTempered(string sOrderCode) : this()
        {
            this._sOrderCode = sOrderCode;
            DataTable dataTable = null;
            Orders orders = new Orders();
            try
            {
                dataTable = orders.LoadOrderByOrderCode(this._sOrderCode);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            orders.Dispose();
            if (dataTable != null)
            {
                if (dataTable.Rows.Count > 0)
                {
                    this._nOrderID = (long)Convert.ToInt32(dataTable.Rows[0]["订单序号"]);
                    this._sCompanyName = dataTable.Rows[0]["公司名称"].ToString();
                    this._sCompanyLinkman = dataTable.Rows[0]["公司联系人"].ToString();
                    this._sProjectName = dataTable.Rows[0]["计划名称"].ToString();
                    this._sProjectLinkman = dataTable.Rows[0]["计划联系人"].ToString();
                    this._sProjectLinkmanPhone = dataTable.Rows[0]["计划联系人电话"].ToString();
                    this._sProjectAddress = dataTable.Rows[0]["计划地址"].ToString();
                    this._sSpeakForDate = dataTable.Rows[0]["订单日期"].ToString();
                    this._sDeliveryDate = DateTime.Parse(dataTable.Rows[0]["交货日期"].ToString()).ToShortDateString();
                    this._isLocked = Convert.ToBoolean(dataTable.Rows[0]["是否锁定"]);
                    this._isCancelled = Convert.ToBoolean(dataTable.Rows[0]["是否作废"]);
                    this._sRemark = dataTable.Rows[0]["订单备注"].ToString();
                    this._sOperatorName = dataTable.Rows[0]["操作者"].ToString();
                    this.txtOrderCode.Text = this._sOrderCode;
                    this.txtCompanyName.Text = this._sCompanyName + "(" + this._sCompanyLinkman + ")";
                    this.txtPorjectName.Text = this._sProjectName + "(" + this._sProjectLinkman + ")";
                    this.txtProjectAddress.Text = this._sProjectAddress + "(" + this._sProjectLinkmanPhone + ")";
                    this.txtSpeakForDate.Text = this._sSpeakForDate + "  交货日期:" + this._sDeliveryDate;
                    this.chkLockedData.Checked = this._isLocked;
                    this.chkCancelled.Checked = this._isCancelled;
                    this.txtRemark.Text = this._sRemark;
                    this.txtOperatorName.Text = this._sOperatorName;
                }
                else
                {
                    MessageBox.Show("没有查询到此订单", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                dataTable.Dispose();
            }
            this.RefreshItems();
            for (int i = 0; i < this.dgProducts.Columns.Count; i++)
            {
                this.dgProducts.Columns[i].ReadOnly = true;
            }
            if (!LoginInfo.Role[PowerName.Invoice_Create])
            {
                this.dgProducts.Columns[9].ReadOnly = false;
            }
            else
            {
                this.dgProducts.Columns[9].ReadOnly = false;
            }
        }

        private void RefreshItems()
        {
            if (this._dtOrderItems != null)
            {
                this._dtOrderItems.Dispose();
                this._dtOrderItems = null;
            }
            OrderItems orderItems = new OrderItems();
            try
            {
                this._dtOrderItems = orderItems.GetOrderItemsForTempered(this._nOrderID);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！\r\n\r\n订单项信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n\r\n订单项信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            orderItems.Dispose();
            if (this._dtOrderItems != null)
            {
                this.dgProducts.DataSource = this._dtOrderItems;
                this.dgProducts.AutoResizeColumns();
            }
            else
            {
                MessageBox.Show("加载此订单的数据失败！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            bool flag = false;
            for (int i = 0; i < this._dtOrderItems.Rows.Count; i++)
            {
                if (this._dtOrderItems.Rows[i]["宽(短边)"].ToString().Length > 0 || this._dtOrderItems.Rows[i]["高(短边)"].ToString().Length > 0)
                {
                    flag = true;
                    this.dgProducts.Columns["宽(短边)"].Visible = true;
                    this.dgProducts.Columns["高(短边)"].Visible = true;
                    break;
                }
            }
            if (!flag)
            {
                this.dgProducts.Columns["宽(短边)"].Visible = false;
                this.dgProducts.Columns["高(短边)"].Visible = false;
            }
            for (int i = 0; i < this.dgProducts.Rows.Count; i++)
            {
                if (Convert.ToInt32(this.dgProducts.Rows[i].Cells["数量"].Value) == Convert.ToInt32(this.dgProducts.Rows[i].Cells["已完成"].Value))
                {
                    this.dgProducts.Rows[i].DefaultCellStyle.BackColor = Color.LawnGreen;
                }
                if (Convert.ToInt32(this.dgProducts.Rows[i].Cells["数量"].Value) < Convert.ToInt32(this.dgProducts.Rows[i].Cells["已完成"].Value))
                {
                    this.dgProducts.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                }
                if (Convert.ToInt32(this.dgProducts.Rows[i].Cells["数量"].Value) > Convert.ToInt32(this.dgProducts.Rows[i].Cells["已完成"].Value) && Convert.ToInt32(this.dgProducts.Rows[i].Cells["已完成"].Value) > 0)
                {
                    this.dgProducts.Rows[i].DefaultCellStyle.BackColor = Color.Orange;
                }
            }
        }

        private void FormOrderTemperedInfo_Load(object sender, EventArgs e)
        {
            this.dgProducts.AutoResizeColumns();
        }

        private void menuRefresh_Click(object sender, EventArgs e)
        {
            this.RefreshItems();
        }

        private void menuShowSubtenseColumn_Click(object sender, EventArgs e)
        {
            if (this._dtOrderItems != null)
            {
                if (this.dgProducts.Columns[5].Visible)
                {
                    this.dgProducts.Columns[5].Visible = false;
                    this.dgProducts.Columns[7].Visible = false;
                }
                else
                {
                    this.dgProducts.Columns[5].Visible = true;
                    this.dgProducts.Columns[7].Visible = true;
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void dgOrders_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            this._sOriginalQuantity = this.dgProducts.Rows[e.RowIndex].Cells[8].Value.ToString().Trim();
            this._sOriginalValueForFinish = this.dgProducts.Rows[e.RowIndex].Cells[9].Value.ToString().Trim();
        }

        private void dgOrders_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            string text = this.dgProducts.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            if (!(this._sOriginalValueForFinish == text))
            {
                if (text == null || !Regex.IsMatch(text, "^[0-9]{1,8}$") || Convert.ToInt32(text) < 0)
                {
                    MessageBox.Show("你所输入的 数量 格式不正确，此处应该为 零 或 正整数！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.dgProducts.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this._sOriginalValueForFinish;
                    this.dgProducts.CurrentCell = this.dgProducts.Rows[e.RowIndex].Cells[e.ColumnIndex];
                }
                else if (Convert.ToInt32(text) > Convert.ToInt32(this._sOriginalQuantity))
                {
                    MessageBox.Show("入库的数量不能超过原始数量！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.dgProducts.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this._sOriginalValueForFinish;
                    this.dgProducts.CurrentCell = this.dgProducts.Rows[e.RowIndex].Cells[e.ColumnIndex];
                }
                else
                {
                    OrderItems orderItems = new OrderItems();
                    try
                    {
                        bool flag = orderItems.SetOrderItemsForTempered(this.dgProducts.Rows[e.RowIndex].Cells[0].Value.ToString(), Convert.ToInt32(this.dgProducts.Rows[e.RowIndex].Cells["已完成"].Value));
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！\r\n\r\n信息可能没更改成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    catch (Exception ex2)
                    {
                        MessageBox.Show(ex2.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    orderItems.Dispose();
                }
            }
        }

        private void dgOrders_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
        }

        private void dgOrders_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show("你输入的 \"" + this.dgProducts.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue.ToString() + " \"可能有误，请检查！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
    }
}
